package cn.fzkj.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.fzkj.gulimall.product.entity.ProductAttrValueEntity;
import cn.fzkj.gulimall.product.service.ProductAttrValueService;
import cn.fzkj.gulimall.product.vo.AttrRespVO;
import cn.fzkj.gulimall.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.fzkj.gulimall.product.service.AttrService;
import cn.fzkj.common.utils.PageUtils;
import cn.fzkj.common.utils.R;



/**
 * 商品属性
 *
 * @author chenxin
 * @email chenxin@gmail.com
 * @date 2022-02-19 22:47:32
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService attrValueService;

    // /product/attr/base/listforspu/${spuId}
    @RequestMapping(value = "/base/listforspu/{spuId}", method = RequestMethod.GET)
    public R getAttrListForSpu(@PathVariable("spuId") Long spuId){

        List<ProductAttrValueEntity> entities = attrValueService.getAttrListForSpu(spuId);
        return R.ok().put("data", entities);
    }

    // /product/attr/update/${spuId}
    @RequestMapping(value = "/update/{spuId}", method = RequestMethod.POST)
    public R updateSpuAttr(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> entities){
        attrValueService.updateSpuAttrs(spuId, entities);

        return R.ok();
    }

    /**
     * 查询属性信息列表【基本属性和销售属性】
     */
    @GetMapping(value = "/{attrType}/list/{catlogId}")
    public R getBaseList(@PathVariable("catlogId") Long catlogId,
                         @RequestParam Map<String, Object> params,
                         @PathVariable("attrType") String attrType){
        PageUtils page = attrService.queryAttrPage(catlogId, params, attrType);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVO respVO = attrService.getAttrInfo(attrId); // 会返回所属分类的path

        return R.ok().put("attr", respVO);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVO attr){
		boolean success = attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVO attr){
//		attrService.updateById(attr);

        boolean success = attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
